package junior.无名;
/**         1   2
 * 123456   1   1
 *    456   0   9
 */
public class 大整数相加 {
    public static void main(String[] args) {
        String s1 = "9999";
        String s2 = "1234";
        String s = bigAdd(s1, s2);
        System.out.println(s1+" + "+s2 +" = "+s);
    }
    public static String bigAdd(String str1, String str2) {
        StringBuffer str4 = new StringBuffer();
        if (str1.length() < str2.length()){
            String str3 = str2;
            str2 = str1;
            str1 = str3;
        }
        int len1 = str1.length(),len2 = str2.length(),differ = len1-len2;
        char[][] ch = new char[2][len1+1];
        ch[0][0] = '0';
        ch[1][0] = '0';
        for (int i = len1; i >= 0; i--) {
            if(i-1>=0)
                ch[0][i] = str1.charAt(i-1);
            if(i-differ > 0)
                ch[1][i] = str2.charAt(i-1-differ);
            int number = (ch[0][i] + ch[1][i] + ch[1][0] - 3*48);  // 余数
            ch[0][i] = (char)(number%10+48);
            ch[1][0] = number >9 ? '1' : '0';
        }
        // if(ch[1][0]=='1')
        //     ch[0][0]='1';
        for (int i = ch[0][0] == '0' ? 1 : 0; i <= len1; i++) {
            str4.append(ch[0][i]);
        }
        return str4.toString(); //01235691356
    }
}
